//
//  ZJStudyClassifyModuleView.m
//  LovingPartyBuilding
//
//  Created by ZJ on 2017/5/2.
//  Copyright © 2017年 刘应平. All rights reserved.
//


#import "ZJStudyClassifyModuleView.h"

@interface ZJStudyClassifyModuleView ()
{
    UIView *_bgView;/**< 上面背景View */
}
/** 红色标记 */
@property (nonatomic, strong) UILabel *redLabel;
/** 分类title */
@property (nonatomic, strong) UILabel *titleLabel;
@end

@implementation ZJStudyClassifyModuleView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor whiteColor];
        _bgView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.width, 37 * [UIScreen zj_screenHScale])];
        _bgView.backgroundColor = [UIColor viewBGGrayColor];
        [self addSubview:_bgView];
        [self addSubview:self.redLabel];
        [self addSubview:self.titleLabel];
    }
    return self;
}


- (void)updateUIWithDataArray:(NSArray *)dataArray{

    CGFloat space = 12 * [UIScreen zj_screenWScale];//所有的间距
    CGFloat buttonWidth = (self.width - 3 * space) / 2;
    CGFloat buttonHeight = 38 * [UIScreen zj_screenHScale];
    
    for (int i = 0; i < dataArray.count; i ++) {
        ZJStudyClassifyButtonModel *model = self.model.modelArray[i];
        
        UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(space + (buttonWidth + space) * (i % 2), space + (buttonHeight + space) * (i / 2) + _bgView.bottom, buttonWidth, buttonHeight)];
        button.tag = i;
        [button setTitle:model.CATEGORY_NAME forState:UIControlStateNormal];
        [button setTitleColor:[UIColor textBlackColor] forState:UIControlStateNormal];
        button.titleLabel.font = [UIFont titleTextFont];
        button.layer.borderColor = [UIColor zj_colorWithHexString:@"c9c9c9"].CGColor;
        button.layer.borderWidth = 0.5 * [UIScreen zj_screenWScale];
        button.layer.cornerRadius = buttonHeight / 8.0;
        [button addTarget:self action:@selector(action_button:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:button];
        self.height = button.bottom + space;
    }
}

- (void)action_button:(UIButton *)button{
    if (self.delegate) {
        [self.delegate ZJStudyClassifyModuleView_ButtonClickToInfo:self.model.modelArray[button.tag]];
    }

}

#pragma mark - Setter
- (void)setModel:(ZJStudyClassifyModuleViewModel *)model{
    _model = model;
    self.titleLabel.text = model.CATEGORY_FNAME;
    [self updateUIWithDataArray:model.modelArray];
}

#pragma mark - Getters
- (UILabel *)redLabel{
    if (_redLabel == nil){
        _redLabel = [[UILabel alloc] initWithFrame:CGRectMake(12 * [UIScreen zj_screenWScale], 0, 5 * [UIScreen zj_screenWScale], 18 * [UIScreen zj_screenHScale])];
        _redLabel.backgroundColor = [UIColor themeColor];
        _redLabel.centerY = _bgView.centerY;
    }
    return _redLabel;
}

- (UILabel *)titleLabel{
    if (_titleLabel == nil){
        _titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(self.redLabel.right + 5 * [UIScreen zj_screenWScale], 0, self.width - self.redLabel.right - 17 * [UIScreen zj_screenWScale], _bgView.height)];
        _titleLabel.textColor = [UIColor textBlackColor];
        _titleLabel.font = [UIFont titleTextFont];
    }
    return _titleLabel;
}

@end
